if(window.plus) {
	plusReady();
} else {
	document.addEventListener('plusready', plusReady, false);
}

function plusReady() {
	$(document).ready(function() {
		date_init();
		UpdateState();
		

		document.addEventListener('UniAppJSBridgeReady', function() {
		// back		
			// document.querySelector('#btn-back').addEventListener('click', function(evt) {		      
			// 	uni.navigateTo({
			// 		url: '/pages/index/main'
			// 	});
			// 	console.log('exit')
			// });
			document.querySelector('#exit').addEventListener('click', function(evt) {
				console.log('exit')
				plus.storage.setItem('vid', '')   
					
					$.cookie("remember", "false", { expire: -1 }); 
					$.cookie("username", "", { expires: -1 }); 
					$.cookie("password", "", { expires: -1 }); 
				uni.redirectTo({
					url: '/pages/login/login'
				});
			});
			var userInfo = getData("user_data");
			if(userInfo == null || userInfo.type != "success") {
				plus.nativeUI.alert("用户没有登录!");
				window.location.href = 'login.html';
			}
			//初始化用户名
			$("#username").append(userInfo.msg.cname);
			
			var date = $(".date_init").val();
			data_init(date, userInfo.msg.username);
			
			$(".form_datetime").on("click", function() {
				plus.nativeUI.pickDate(function(e) {
					var d = e.date;
					$(".form_datetime").val(date_formate(d));
					date = $(".date_init").val();
					data_init(date, userInfo.msg.username);
				}, function(e) {
					plus.nativeUI.alert("未选择日期");
				});
			});
		});
		// back end
		

		
		//查询按钮
		$("#date_submit").on("click", function() {
			date = $(".date_init").val();
			data_init(date, userInfo.msg.username);
		});

	});

	function data_init(date, username) {
		UpdateState();
		if(!isOnLine) {
			loadInfo();
		} else {
			var url = server_url + '/9airweb/CabinMap.action?username=' + username + '&flightdate=' + date;
			url += url_tail;
			console.log(url);
			$(".list-group").hide();
			$("#caseVerteClaire").show();

			$.ajax({
				dataType: "json",
				url: url,
				timeout: 12000
			}).done(function(backdata, status) {
				if(status == 'success') {
					// console.log(backdata.msg)
					if(backdata.type == "error") {
						plus.nativeUI.alert(backdata.msg);
						loadInfo();
						//		          if (setData("FlightInfo" + date + username, data)) {
						//						loadInfo();
						//					}
					} else {
						dataInit_psg(backdata);
					}

				}
			}).fail(function(jqXHR, textStatus) {
				if(textStatus == 'timeout') {
					plus.nativeUI.alert("数据更新失败，即将加载本地数据！");
					loadInfo();
				}
			});
		}
		//加载过站人数
		function dataInit_psg(data) {
			UpdateState();
			if(!isOnLine) {
				loadInfo();
			} else {
				var url = server_url + '/9airweb/loadVirtualPsgcount?username=' + username + '&flightdate=' + date;
				url += url_tail;
				console.log(url);
				$(".list-group").hide();
				$("#caseVerteClaire").show();

				$.ajax({
					dataType: "json",
					url: url,
					timeout: 12000
				}).done(function(backdata, status) {
					if(status == 'success' && backdata.type == 'success') {
						var datas = [];
						for(var key in backdata.msg) {
							var msg1 = backdata.msg[key];
							for(var i in msg1) {
								console.log(msg1)
								console.log(msg1[i])
								var adultCount = msg1[i].aAdult + msg1[i].bAdult + msg1[i].cAdult;
								var infCount = msg1[i].aInfant + msg1[i].bInfant + msg1[i].cInfant;
								var childCount = msg1[i].aChild + msg1[i].bChild + msg1[i].cChild;
								var totleCount = adultCount + childCount + infCount;
								var psgData = new Object();
								psgData.adultCount = adultCount;
								psgData.infCount = infCount;
								psgData.childCount = childCount;
								psgData.totleCount = totleCount;
								psgData.arrivalAirport = msg1[i].arrivalAirport;
								psgData.departureAirport = msg1[i].departureAirport;
								psgData.flightNo = msg1[i].flightNo;
								datas.push(psgData);
							}
						}

						//遍历已经存在的数据
						$.each(data.msg, function(i, data1) {
							var orgi = data1.orgi.split("/").pop();
							var flightNo = data1.flightNo;
							var dest = data1.dest.split("/").pop();
							$.each(datas, function(index, psg) {
								if(flightNo == psg.flightNo && orgi == psg.departureAirport) {
									if(dest == psg.arrivalAirport) {
										data1["adultCount1"] = psg.adultCount;
										data1["infCount1"] = psg.infCount;
										data1["childCount1"] = psg.childCount;
										data1["totleCount1"] = psg.totleCount;
									} else {
										data1["adultCount3"] = psg.adultCount;
										data1["infCount3"] = psg.infCount;
										data1["childCount3"] = psg.childCount;
										data1["totleCount3"] = psg.totleCount;
									}
								} else if(flightNo == psg.flightNo && dest == psg.arrivalAirport) {
									if(orgi == psg.departureAirport) {
										data1["adultCount1"] = psg.adultCount;
										data1["infCount1"] = psg.infCount;
										data1["childCount1"] = psg.childCount;
										data1["totleCount1"] = psg.totleCount;
									} else {
										data1["adultCount3"] = psg.adultCount;
										data1["infCount3"] = psg.infCount;
										data1["childCount3"] = psg.childCount;
										data1["totleCount3"] = psg.totleCount;
									}
								}
							});

						});
						//add 
						if(setData("FlightInfo" + date + username, data)) {
							loadInfo();
						}
					}
				}).fail(function(jqXHR, textStatus) {
					if(textStatus == 'timeout') {
						plus.nativeUI.alert("数据更新失败，即将加载本地数据！");
						loadInfo();
					}
				});
			}
		}

		function loadInfo() {
			var info = getData("FlightInfo" + date + username);
			//初始化数据
			flight_init(info);
			$("a.list-group-item").on("mouseover", function() {
				$(".active").removeClass("active");
				$(this).addClass("active");
			});
			$("a.list-group-item").on("touchstart", function() {
				$(".active").removeClass("active");
				$(this).addClass("active");
			});
		}
	}

	//加载查询的数据
	function flight_init(info) {
		$(".list-group").show();
		$(".list-group").empty();
		$("#caseVerteClaire").hide();

		if(info.type != "success") {
			plus.nativeUI.alert(info.msg);
		} else {
			$.each(info.msg, function(i, data) {
				var html = '<div class="btn-group flight-list">' +
					'<a href="javascript:void(0)" class="list-group-item info_detail" id="active" data-flightId=' + data.flightId + ' data-flightNo=' + data.flightNo + ' data-aircTailNum=' + data.aircTailNum + ' data-legTh=' + data.legTh + '>' +
					'<h4 class="list-group-item-heading">' +
					'<div class="row" style="text-align:center">' +
					'<div class="col-xs-2 col-sm-2 col-md-2">' +
					'<span>' + data.flightNo + '</span>' +
					'</div>' +
					'<div class="col-xs-3 col-sm-3 col-md-3">' +
					'<span>日期</span>' +
					'</div>' +
					'<div class="row col-xs-7 col-sm-7 col-md-7">' +
					'<div class="col-xs-5 col-sm-5 col-md-5">' +
					'<span>' + data.orgi + '</span>' +
					'</div>' +
					'<div class="col-xs-2 col-sm-2 col-md-2">' +
					'<span>&rarr;</span>' +
					'</div>' +
					'<div class="col-xs-5 col-sm-5 col-md-5">' +
					'<span>' + data.dest + '</span>' +
					'</div>' +
					'</div>' +
					'</div>' +
					'</h4>' +
					'<p class="list-group-item-text">' +
					'<div class="row" style="text-align:center">' +
					'<div class="col-xs-2 col-sm-2 col-md-2">' +
					'<span>' + data.flightStatus + '</span>' +
					'</div>' +
					'<div class="col-xs-3 col-sm-3 col-md-3">' +
					'<span>' + data.flightDate + '</span>' +
					'</div>' +
					'<div class="row col-xs-7 col-sm-7 col-md-7">' +
					'<div class="col-xs-5 col-sm-5 col-md-5">' +
					'<span>' + data.origtime + '</span>' +
					'</div>' +
					'<div class="col-xs-2 col-sm-2 col-md-2">' +
					'<span></span>' +
					'</div>' +
					'<div class="col-xs-5 col-sm-5 col-md-5">' +
					'<span>' + data.destime + '</span>' +
					'</div>' +
					'</div>' +
					'</div>' +
					'</p>' +
					'<hr class="">' +
					'<p class="list-group-item-text">' +
					'<div class="row" style="text-align:center">' +
					'<div class="col-xs-3 col-sm-3 col-md-3">' +
					'<span>舱单总人数</span>' +
					'</div>' +
					'<div class="col-xs-3 col-sm-3 col-md-3">' +
					'<span>成人</span>' +
					'</div>' +
					'<div class="col-xs-3 col-sm-3 col-md-3">' +
					'<span>儿童</span>' +
					'</div>' +
					'<div class="col-xs-3 col-sm-3 col-md-3">' +
					'<span>婴儿</span>' +
					'</div>' +
					'</div>' +
					'<div class="row" style="text-align:center">' +
					'<div class="col-xs-3 col-sm-3 col-md-3">' +
					'<span>' + (data.ad + data.chd + data.inf) + '</span>' +
					'</div>' +
					'<div class="col-xs-3 col-sm-3 col-md-3">' +
					'<span>' + data.ad + '</span>' +
					'</div>' +
					'<div class="col-xs-3 col-sm-3 col-md-3">' +
					'<span>' + data.chd + '</span>' +
					'</div>' +
					'<div class="col-xs-3 col-sm-3 col-md-3">' +
					'<span>' + data.inf + '</span>' +
					'</div>' +
					'</div>'
					//add row
					+
					'<div class="row" style="text-align:center">' +
					'<div class="col-xs-3 col-sm-3 col-md-3">' +
					'<span>' + (typeof(info.msg[i].totleCount1) == "undefined" ? 0 : info.msg[i].totleCount1) + "/" + (typeof(info.msg[i].totleCount3) == "undefined" ? 0 : info.msg[i].totleCount3) + '</span>' +
					'</div>' +
					'<div class="col-xs-3 col-sm-3 col-md-3">' +
					'<span>' + (typeof(info.msg[i].adultCount1) == "undefined" ? 0 : info.msg[i].adultCount1) + "/" + (typeof(info.msg[i].adultCount3) == "undefined" ? 0 : info.msg[i].adultCount3) + '</span>' +
					'</div>' +
					'<div class="col-xs-3 col-sm-3 col-md-3">' +
					'<span>' + (typeof(info.msg[i].childCount1) == "undefined" ? 0 : info.msg[i].childCount1) + "/" + (typeof(info.msg[i].childCount3) == "undefined" ? 0 : info.msg[i].childCount3) + '</span>' +
					'</div>' +
					'<div class="col-xs-3 col-sm-3 col-md-3">' +
					'<span>' + (typeof(info.msg[i].infCount1) == "undefined" ? 0 : info.msg[i].infCount1) + "/" + (typeof(info.msg[i].infCount3) == "undefined" ? 0 : info.msg[i].infCount3) + '</span>' +
					'</div>' +
					'</div>'
					//add row
					+
					'</p>' +
					'</a>' +
					'</div>';
				$(".list-group").prepend(html);
			});

		}

		$("#active").addClass("active");

		$(".info_detail").on("click", function() {
			var flightId = $(this).attr("data-flightId");
			var legTh = $(this).attr("data-legTh");
			var flightNo = $(this).attr("data-flightNo");		
			var aircTailNum = $(this).attr("data-aircTailNum");

			setData("flightNo", flightNo);
			setData("flightId", flightId);
			setData("legTh", legTh);
			setData("aircTailNum", aircTailNum);
			setData("date", $(".date_init").val());
			console.log(flightNo, 'flightNo')
			// 拦截 跳转入新系统
			setTimeout(()=> {
				uni.navigateTo({
					url: '/pages/index/main'
				})		
			}, 100)
			// window.location.href = "flight.html";

			//			setData("flightId","134650");
			//			setData("legTh","2");
			//			window.location.href = "flight.html";

			//          setData("flightId","137894");
			//			setData("legTh","1");
			//			window.location.href = "flight.html";
		});
	};

	function date_init() {
		//初始化选择日期
		$(".date_init").val(function() {
			return date_formate(new Date());
		});
	}
}